package com.hc.controller;


import com.github.pagehelper.PageInfo;
import com.hc.entity.RRepairPool;
import com.hc.service.IAuthorizationService;
import com.hc.service.IRepairPoolService;
import com.hc.util.Constant;
import com.hc.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pool")
@CrossOrigin
public class RepairPoolController {

    IRepairPoolService service;
    IAuthorizationService authorizationService;

    @Autowired
    public void setAuthorizationService(IAuthorizationService authorizationService) {
        this.authorizationService = authorizationService;
    }

    @Autowired
    public void setService(IRepairPoolService service) {
        this.service = service;
    }

    @GetMapping
    @ApiOperation(value = "获取工单", produces = MediaType.APPLICATION_JSON_VALUE, response = GetRepairPoolVO.class, httpMethod = "GET")
    public StatusVO getRepairPoolList(@RequestParam int pageSize,
                                      @RequestParam int pageNum,
                                      @RequestParam(required = false) String typeId,
                                      @RequestParam(required = false) String tel,
                                      @RequestParam(required = false) String repairId,
                                      @RequestParam(required = false) String repairName,
                                      @RequestParam(required = false) String state) {
        RRepairPool condition = new RRepairPool();
        condition.setRepairType(typeId);
        condition.setRepairName(repairName);
        condition.setState(state);
        condition.setRepairId(repairId);
        condition.setTel(tel);
        PageInfo<RRepairPool> pageInfo = service.getRepairByConditions(pageNum, pageSize, condition);
        PagingVO<GetRepairPoolVO> result = new PagingVO<>();
        result.setMaxRows(pageInfo.getTotal());
        result.setData(pageInfo.getList().stream().map(
                ent -> {
                    GetRepairPoolVO vo = new GetRepairPoolVO();
                    BeanUtils.copyProperties(ent, vo);
                    return vo;
                }).collect(Collectors.toList()));
        return new StatusVO(result);
    }


    @PutMapping("/{ID}")
    @ApiOperation(value = "修改工单内容", consumes = MediaType.APPLICATION_JSON_VALUE, httpMethod = "PUT")
    public StatusVO update(@PathVariable String ID, @RequestBody @Valid UpdateRepairPoolVO vo) {
        RRepairPool pool = new RRepairPool();
        pool.setRepairId(ID);
        vo.setAppointmentTime(Timestamp.from(vo.getAppointmentTime().toInstant()));
        BeanUtils.copyProperties(vo, pool);
        if (service.updateRepairPool(pool) <= 0) {
            return new StatusVO();
        }
        return new StatusVO();
    }

    @PostMapping("/tickets")
    @ApiOperation(value = "添加工单", code = 201, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE, response = StatusVO.class)
    public StatusVO addTickets(@RequestBody @Valid AddTicketVO vo) {
        service.dispatchTickets(vo.getRepairId(), vo.getStaffId());
        return new StatusVO(201);
    }

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping
    @ApiOperation(value = "登记电话报修",
            produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE, code = 201)
    public StatusVO addRegistration(@RequestBody @Valid AddRepairRegistrationVO vo, @RequestHeader(Constant.AUTTH_HEADER_NAME) String token) throws ParseException {
        RRepairPool pool = new RRepairPool();
        //这样就可以拷贝进去了
        BeanUtils.copyProperties(vo, pool);
        pool.setAppointmentTime(Timestamp.from(vo.getAppointmentTime().toInstant()));
        pool.setCommunityId(authorizationService.getCommunityId(token));
        service.registerRepair(pool);
        return new StatusVO(201);
    }


}
